<style>
.glogo{width: 100px;}
.hide{
    position: absolute;
    top:0;
    left:0;
    width: 100%;
    height: 100%;
    z-index: 9;
    opacity: 0;
}
</style>
<div class="container-fluid" id='app'>
    <div class="row">
        <div class="col-lg-12">
        	<ul class="nav nav-tabs">
                <li class="nav-item">
                    <a class="nav-link @if($type==0) active @endif" href="/SPAdmin/good">话费设置</a>
                </li>
                 <li class="nav-item">
                    <a class="nav-link @if($type==1) active @endif" href="/SPAdmin/good?type=1">电费设置</a>
                </li>
                 <li class="nav-item">
                    <a class="nav-link @if($type==2) active @endif" href="/SPAdmin/good?type=2">燃气设置</a>
                </li>
                 <li class="nav-item">
                    <a class="nav-link @if($type==10) active @endif" href="/SPAdmin/good?type=10">其他商品</a>
                </li>
            </ul>
        	<a v-if='type < 10' @click='onAdd(false)' style='margin-bottom: 10px;' class='btn btn-primary'>添加面值</a>
        	<div class="card" style="margin-top: 10px;" v-else>
                <div class="card-header bg-primary text-white">筛选</div>
                <div class="card-body">
                    <div class="row g-3 form-horizontal" role="form">
                        <div class="form-group">
                            <div class="row">
                            	 <label class="control-label" style="width: 80px;">商品分类</label>
                                <div class="col-md-2">
                                    <select class='form-control' v-model='search.cid' data-live-search='true'>
                                        <option value='0'>全部分类</option>
                                        <option v-for='c in cate' :value='c.id'>{c.title}</option>
                                    </select>
                                </div>
                                <div class="col-md-2">
                                    <button class="btn btn-info btn-sm" @click='onCate'>分类管理</button>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="row">
                            	<label class="control-label" style="width: 80px;">商品名称</label>
                                <div class="col-md-2">
                                    <input class="form-control" v-model='search.title' placeholder="请输入商品名称">
                                </div>
                                <div class="col-md-2">
                                    <button class="btn btn-default" onclick="onSearch"><i class="fa fa-search"></i> 搜索</button>
                                    <button class="btn btn-primary" @click='onAdd(false)'>新增商品</button>
                                </div>
                        	</div>
						</div>
                    </div>
                </div>
        	</div>
        	<div class="row" v-if='type < 10 && list'>
        		<div class="col-sm-4" v-for='(v,kk) in list'>
                    <div class="card">
                    	<h5 class="card-header" style='display: flex;align-items: center;justify-content: space-between;'>
                    		<div>
                    		{v.title}元{{$type==1?'电费':($type == 2 ? '燃气费' : '话费')}} 
                    		<span class="badge bg-success" @click='onGStatus(v)' v-if='v.status == 0'>上架</span>
                        	<span class="badge bg-warning" @click='onGStatus(v)' v-else>下架</span>
                    		</div>
                    		<div class="btn-group" role="group">
                                <button @click='onAdd(v,kk)' type="button" class="btn btn-default btn-sm">编辑</button>
                                <button @click='onGDel(kk)' type="button" class="btn btn-danger btn-sm">删除</button>
                              </div>
                    	</h5>
                        <div class="card-body">
                        	<div class="mb-3" style='display: flex;justify-content: space-between;'>
                              <label for="exampleInputEmail1" class="form-label">销售加价</label><span>{v.marketprice}元</span>
                            </div>
                        	<div class="mb-3">
                              <label for="exampleInputEmail1" class="form-label">代理加价</label>
                              <div class='input-group' v-for='(a,k) in v.agentprice'> 
                                  <div class='input-group-text'>{levels[k]}</div> 
                              	  <input readonly="readonly" class='form-control' :value="'+'+a">
                                  <div class='input-group-text'>元</div> 
                              </div>
                            </div>
                            <div class="table-responsive" v-if='v.items && v.items.length > 0'>
                              <table class="table table-hover">
                                <thead>
                                  <tr>
                                    <th>渠道</th>
                                    <th>商品</th>
                                    <th>价格</th>
                                    <th>状态</th>
                                    <th>删除</th>
                                  </tr>
                                </thead>
                                <tbody>
                                  <tr v-for='(d,dk) in v.items'>
                                    <td>{channel[d.cid]}</td>
                                    <td>{d.itemno} <i class='mdi mdi-help-circle' @click='onTip(d.title)'></i></td>
                                    <td>{d.costprice}</td>
                                    <td>
                                    	<span class="badge bg-success" v-if='d.status == 0'>上架</span>
                                    	<span class="badge bg-warning" v-else>下架</span>
                                   	</td>
                                    <td>
                                      <a class='btn btn-danger btn-sm' @click='onCDel(kk,dk,d)'>×</a>
                                    </td>
                                  </tr>
                                </tbody>
                              </table>
                            </div>
                        	<a class='btn btn-primary btn-sm' @click='onBind(kk,v)'>绑定渠道</a>
                        </div>
                    </div>
        		</div>
        	</div>
        	<div class="row" v-else-if='list'>
        		<div class="col-lg-12">
            		<div class="card col-sm-12" style="display: inline-block;margin: 10px;" v-for='(v,kk) in list'>
                        	<h5 class="card-header" style='display: flex;align-items: center;justify-content: space-between;'>
                        		<div>
                        		<img :src='v.logo' style="width: 50px;height: 50px;"> {v.title}
                        		<span class="badge bg-success" @click='onGStatus(v)' v-if='v.status == 0'>上架</span>
                            	<span class="badge bg-warning" @click='onGStatus(v)' v-else>下架</span>
                        		</div>
                        		<div class="btn-group" role="group">
                                    <button @click='onAddItem(kk,v)' type="button" class="btn btn-primary btn-sm">添加规格</button>
                                    <button @click='onAdd(v,kk)' type="button" class="btn btn-default btn-sm">编辑</button>
                                    <button @click='onGDel(kk)' type="button" class="btn btn-danger btn-sm">删除</button>
                                  </div>
                        	</h5>
                            <div class="card-body">
                            	 <table class='table' v-if='v.items && v.items.length > 0'>
                            	 	<thead><tr><th>规格名</th><th>规格项</th><th style="width: 200px;">规格加价</th><th style="text-align: center;">价格</th><th>状态</th><th>渠道</th><th>操作</th></tr></thead>
                        	 		<tbody v-for='(option,ok) in v.items'>
                            	 		<tr v-for='(op,opk) in option.items'>
                            	 			<td v-if='opk == 0' :rowspan="option.items.length" style="vertical-align: middle;">{option.title}</td>
                            	 			<td style="vertical-align: middle;">{op.title}</td>
                            	 			<td>
                            	 				<div class='input-group' v-for='(p,pk) in op.agentprice'>
                            	 					<div class='input-group-text'>{levels[pk]}</div>
                            	 					<input class='form-control' :value='"+"+p' readonly="readonly">
                            	 					<div class='input-group-text'>元</div>
                            	 				</div>
                            	 				<div class='input-group'>
                            	 					<div class='input-group-text'>销售加价</div>
                            	 					<input class='form-control' :value='"+"+op.marketprice' readonly>
                            	 					<div class='input-group-text'>元</div>
                            	 				</div>
                            	 			</td>
                            	 			<td style="text-align: center;">原价：￥{op.oldprice}<br>当前：{op.cprice ? op.cprice : '-'}</td>
                            	 			<td><span @click='onOStatus(kk,dk,op)'  class="badge bg-success" v-if='op.status == 0'>上架</span><span v-else @click='onOStatus(kk,dk,op)'  class="badge bg-warning">下架</span></td>
                            	 			<td>{op.cnum}个</td>
                            	 			<td>
                            	 				<div class="btn-group" role="group">
                                	 				<a class='btn btn-primary btn-sm' @click='onItemBind(op)'>渠道绑定</a>
                                	 				<a class='btn btn-default btn-sm' @click='onItemEdit(kk,ok,opk)'>编辑</a>
                                	 				<a class='btn btn-danger btn-sm' @click='onItemDel(kk,ok,opk)'>删除</a>
                            	 				</div>
                            	 			</td>
                            	 		</tr>
                            	 	</tbody>
                            	 </table>
                            </div>
                       </div>
                 </div>
        	</div>
        </div>
    </div>
    <div class="modal fade" id="addItem" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLiveLabel" aria-hidden="true">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <h6 class="modal-title" id="staticBackdropLiveLabel">规格设置</h6>
              <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body" v-if='gooditem' style="max-height: 800px;overflow-y: scroll;">
                    <div class="mb-3 row">
                        <div class='input-group'>
              				<div class='input-group-text'>规格名称</div>
          					<input class='form-control' v-model='gooditem.title' :readonly='gooditem.isedit'>
      					</div>
                    </div>
                    <div class="mb-3 row">
                      <label class="col-sm-2 col-form-label">规格项</label>
                      <div>
                      		<div class='card' v-for='m in gooditem.items'>
                      			<div class='card-body'>
                      				<div class='input-group'>
                          				<div class='input-group-text'>规格项名</div>
                      					<input class='form-control' v-model='m.title'>
                      				</div>
                      				<div class='input-group'>
                          				<div class='input-group-text'>市场原价</div>
                      					<input class='form-control' v-model='m.oldprice'>
                          				<div class='input-group-text'>元</div>
                      				</div>
                      				<div class='input-group' v-for='(p,l) in m.agentprice'>
                          				<div class='input-group-text'>{levels[l]}加价</div>
                      					<input class='form-control' v-model='m.agentprice[l]'>
                      					<div class='input-group-text'>元</div>
                      				</div>
                      				<div class='input-group'>
                          				<div class='input-group-text'>销售加价</div>
                      					<input class='form-control' v-model='m.marketprice'>
                      					<div class='input-group-text'>元</div>
                      				</div>
                      			</div>
                      		</div>
                      		<a class='btn btn-warning btn-sm' style="float: right;" @click='onAddItem2' v-if='!gooditem.isedit'>添加规格项</a>
                      </div>
                    </div>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
              <button type="button" class="btn btn-primary" @click='onItemSave'>保存</button>
            </div>
          </div>
        </div>
      </div>
    <div class="modal fade" id="channelItem" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLiveLabel" aria-hidden="true">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <h6 class="modal-title" id="staticBackdropLiveLabel">渠道设置</h6>
              <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body" style="max-height: 800px;overflow-y: scroll;" v-if='cgood'>
            		<div class="mb-3 row" style="padding: 0 10px;">
            			<div class='card' v-for='(v,k) in cgood.list'>
                			<div class='card-header' style="display: flex;align-items: center;">
                				{channel[v.cid]}
                				<span class="badge bg-success">上架</span>
                				<a class='btn btn-danger btn-sm' style="margin-left: auto;" @click='doItemDel(k,v)'>删除</a>
                			</div>
                			<div class='card-body'>
            					【{v.itemno}】{v.title}
            					<br>￥{v.costprice}
            				</div>
            			</div>
            		</div>
                    <div class="mb-3 row">
                        <div class='input-group'>
              				<div class='input-group-text'>选择渠道</div>
              				<select class='form-control' v-model='cgood.cid'>
              					<option value='0'>请选择渠道</option>
              					<option v-for='(c,k) in channel' :value='k'>{c}</option>
              				</select>
              				<div class='input-group-text'>渠道商品ID</div>
          					<input class='form-control' v-model='cgood.itemno'>
              				<a class='btn btn-info' @click='doItemBind'>绑定</a>
      					</div>
                    </div>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
              <a type="button" class="btn btn-warning" href='/SPAdmin/good?type=10'>立即刷新</a>
            </div>
          </div>
        </div>
      </div>
     <div class="modal fade" id="cateview" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLiveLabel" aria-hidden="true">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <h6 class="modal-title" id="staticBackdropLiveLabel">分类设置</h6>
              <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                    <div class="mb-3 row">
                        <div class='input-group' v-for='(c,k) in tmpcate' style="margin-bottom: 5px;">
              				<div class='input-group-text'>分类名称</div>
          					<input class='form-control' v-model='c.title'>
              				<a class='btn btn-danger' @click='doCateDel(k)'>删除</a>
      					</div>
                    </div>
                    <a class='btn btn-warning btn-sm' style="float: right;" @click='onCateAdd'>添加分类</a>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
              <button type="button" class="btn btn-primary" @click='onCateSave' v-if='tmpcate.length > 0'>保存</button>
            </div>
          </div>
        </div>
      </div>
</div> 
<style>
</style>

<script src="/assets/js/vue.js"></script>
<script>
var app = new Vue({
	el: '#app',
	delimiters:['{','}'],
	data: {
		type:{{$type}},
		list:false,
		search:{cid:0,title:''},
		levels:{},
		channel:{},
		tempgood:false,
		gooditem:false,
		cgood:false,
		cate:[],
		tmpcate:[],
	},
	created(){
    	var t = this;
    	lightyear.loading('show');
        $.ajax({
        	url:"",
        	type:'post',
        	dataType:'json',
        	success:function(res){
        		lightyear.loading('hide');
        		t.levels = res.levels;
        		t.channel = res.channel;
        		t.list = res.list;
        		t.cate = res.cate;
        	}
        });
	},
	methods: {
		onSearch(){
			var t = this;
	    	lightyear.loading('show');
	        $.ajax({
	        	url:"",
	        	type:'post',
	        	dataType:'json',
	        	data:this.search,
	        	success:function(res){
	        		lightyear.loading('hide');
	        		t.list = res.list;
	        	}
	        });
		},
		onCate(){
			if(this.cate.length > 0) this.tmpcate = JSON.parse(JSON.stringify(this.cate));
			else this.tmpcate = [{title:''}];
			$('#cateview').modal('show');
		},
		onCateAdd(){
			this.tmpcate.push({title:""})
		},
		onCateSave(){
			var res = difference(this.tmpcate,this.cate);
			var t = this;
			showLoading();
            $.ajax({
            	url:"/SPAdmin/good/cate",
            	type:'post',
            	dataType:'json',
            	data: {op:'add',data:res},
            	success:function(res){
            		hideLoading();
            		if(res.code == 1){
            			$.notify('操作成功');
            			t.cate = res.cate;
            			$('#cateview').modal('hide');
            		}else $.notify(res.msg);
            	}
            });
		},
		doCateDel(k){
			var cid = this.tmpcate[k].id;
			if(!cid) return this.tmpcate.splice(k,1);
			var t = this;
			$.confirm({
				title:'温馨提示',
				content: '确认删除该分类吗？',
		        buttons:{
		        	confirm:{ 
		        		text:'确认',
		        		btnClass: "btn-primary",
		        		action: function(){
		        			showLoading();
			                $.ajax({
			                	url:"/SPAdmin/good/cate",
			                	type:'post',
			                	dataType:'json',
			                	data: {op:'del',cid:cid},
			                	success:function(res){
			                		hideLoading();
			                		if(res.code == 1){
			                			$.notify('操作成功');
			                			t.tmpcate.splice(k,1);
			                		}else $.notify(res.msg);
			                	}
			                });
			            },
		        	},
		        	cancel:{text:'取消'}
		        }
			});
		},
		onAddItem(kk,v){
			this.tempgood = v;
			this.tempgood.index = kk;
			this.gooditem = {title:'',items:[this.initItem()]};
			$('#addItem').modal('show');
		},
		onAddItem2(){
			this.gooditem.items.push(this.initItem())
		},
		onItemEdit(kindex,oindex,index){
			this.tempgood = this.list[kindex];
			this.tempgood.index = kindex;
			var item = JSON.parse(JSON.stringify(this.list[kindex].items[oindex].items[index]));
			this.gooditem = {isedit:1,title:item.ptitle,items:[item],oindex:oindex,index:index};
			$('#addItem').modal('show');
		},
		onItemSave(){
			if(this.gooditem.title == '') return $.notify('请输入规格名称');
			if(this.gooditem.items[0].title == '') return $.notify('请输入规格项名称');
			var t = this;
			showLoading();
            $.ajax({
            	url:"/SPAdmin/good/post",
            	type:'post',
            	dataType:'json',
            	data:{op:'item',type:this.type,gid:this.tempgood.id,data:JSON.stringify(this.gooditem)},
            	success:function(res){
            		hideLoading();
            		if(res.code == 1){
            			$.notify('操作成功');
            			if(t.gooditem.isedit){
            				t.list[t.tempgood.index].items[t.gooditem.oindex].items[t.gooditem.index] = t.gooditem.items[0];
            				t.$forceUpdate();
            			}else{
                            if(!t.list[t.tempgood.index].items) t.$set(t.list[t.tempgood.index],'items',[]);
                            else{
    							for(var i in t.tempgood.items){
    								if(t.tempgood.items[i].title == t.gooditem.title){
    									t.list[t.tempgood.index].items[i].items = t.list[t.tempgood.index].items[i].items.concat(res.item.items);
    									return $('#addItem').modal('hide');
    								}
    							}
                            }
                			t.list[t.tempgood.index].items.push(res.item)
            			}
            			$('#addItem').modal('hide');
            		}else $.notify(res.msg);
            	}
            });
		},
		initItem(){
			var item = {title:'',agentprice:{},marketprice:'',oldprice:''};
			for(var i in this.levels){
				item.agentprice[i] = '';
			}
			return item;
		},
		onOStatus(kk,dk,item){
			showLoading();
            $.ajax({
            	url:"/SPAdmin/good",
            	type:'post',
            	dataType:'json',
            	data: {gid:item.id,op:'ostatus',type:this.type},
            	success:function(res){
            		hideLoading();
            		if(res.code == 1){
            			$.notify('操作成功');
            			item.status = item.status == 0 ? 1 : 0;
            		}else $.notify(res.msg);
            	}
            });
		},
		onItemDel(k,oindex,index){
			var t = this;
			$.confirm({
				title:'温馨提示',
				content: '确认删除该规格项吗？',
		        buttons:{
		        	confirm:{ 
		        		text:'确认',
		        		btnClass: "btn-primary",
		        		action: function(){
		        			showLoading();
			                $.ajax({
			                	url:"/SPAdmin/good/del",
			                	type:'post',
			                	dataType:'json',
			                	data: {op:'option',oid:t.list[k].items[oindex].items[index].id},
			                	success:function(res){
			                		hideLoading();
			                		if(res.code == 1){
			                			$.notify('操作成功');
			                			t.list[k].items[oindex].items.splice(index,1);
			                		}else $.notify(res.msg);
			                	}
			                });
			            },
		        	},
		        	cancel:{text:'取消'}
		        }
			});
		},
		onAdd(item,k){
			if(this.type > 2){
				var str = "<form>";
				str += "<input type='hidden' name='type' value='"+this.type+"'>";
				if(item.id > 0) str += "<input type='hidden' name='gid' value='"+item.id+"'>";
				str += '<div class="input-group" style="margin-bottom: 10px;">'
				str += '<div class="input-group-text">商品名称</div>'
				str += '<input type="text" class="form-control" name="title" value="'+(item.id ? item.title : '')+'">'
				str += '</div>'
		    	str += '<div class="input-group" style="margin-bottom: 10px;">'
		    	str += '<div class="input-group-text">商品LOGO</div>'
	    		if(item && item.logo) str += "<div style='position: relative;'><img class='glogo' style='width: 150px;' src='"+item.logo+"'><input type='hidden' name='logo' value='"+item.logo+"'><input type='file' accept='image/*' class='hide' onchange='onUrl(this,2)' name='logo'></div>";
	    	    else str += '<input type="file" class="form-control glogo" onchange="onUrl(this)" name="logo">'
		    	str += '</div>'
	    		str += '<div class="input-group" style="margin-bottom: 10px;">'
				str += '<div class="input-group-text">商品分类</div>'
				str += "<select class='form-control' name='cid'>";
				for(var i in this.cate) str += "<option value='"+this.cate[i].id+"' "+(this.cate[i].id == item.cid ? 'selected' : '')+">"+this.cate[i].title+"</option>"
			    str += '</select>'
				str += '</div>'
	    		str += '<div class="input-group" style="margin-bottom: 10px;">'
				str += '<div class="input-group-text">商品说明</div>'
				str += '<textarea class="form-control" name="rule" style="min-height: 200px;">'+(item.id ? item.rule : '')+"</textarea>";
				str += '</div>'
		    	str += '</form>'
	    		var t = this;
				return $.confirm({
					title:'设置商品',
					content: str,
			        buttons:{
			        	confirm:{ 
			        		text:'确认',
			        		btnClass: "btn-primary",
			        		action: function(){
			        			showLoading();
				                $.ajax({
				                	url:"/SPAdmin/good/post",
				                	type:'post',
				                	dataType:'json',
				                	data:this.$content.find('form').serialize(),
				                	success:function(res){
				                		hideLoading();
				                		if(res.code == 1){
				                			$.notify('操作成功');
				                			if(item.id > 0){
					                			t.list[k].title = res.item.title;
					                			t.list[k].logo = res.item.logo;
					                			t.list[k].cid = res.item.cid;
					                			t.list[k].rule = res.item.rule;
				                			}else t.list.push(res.item);
				                			t.$forceUpdate();
				                		}else $.notify(res.msg);
				                	}
				                });
				            },
			        	},
			        	cancel:{text:'取消'}
			        }
				});
			}
			
			if(!item) item = {};
			if(!item.agentprice) item.agentprice = {};
			for(var i in this.levels){
				if(!item.agentprice[i]) item.agentprice[i] = '';
			}
			var str = "<form>";
			str += "<input type='hidden' name='type' value='"+this.type+"'>";
			if(item.id > 0) str += "<input type='hidden' name='gid' value='"+item.id+"'>";
			str += '<div class="input-group" style="margin-bottom: 10px;">'
			str += '<div class="input-group-text">面值</div>'
			str += '<input type="number" maxlength="6" min="1" class="form-control" name="title" value="'+(item.id ? item.title : '')+'">'
			str += '</div>'
			for(var i in this.levels){
    			str += '<div class="input-group" style="margin-bottom: 10px;">'
    			str += '<div class="input-group-text">'+this.levels[i]+'加价</div>'
    			str += '<input type="number" class="form-control" name="agentprice['+i+']" value="'+(item.id ? item.agentprice[i] : '')+'">'
    			str += '</div>'
			}
	    	str += '<div class="input-group">'
	    	str += '<div class="input-group-text">销售加价</div>'
	    	str += '<input type="number" class="form-control" name="marketprice" value="'+(item.id ? item.marketprice : '')+'">'
	    	str += '</div></form>'
		    var t = this;
			$.confirm({
				title:'设置面值',
				content: str,
		        buttons:{
		        	confirm:{ 
		        		text:'确认',
		        		btnClass: "btn-primary",
		        		action: function(){
		        			showLoading();
			                $.ajax({
			                	url:"/SPAdmin/good/post",
			                	type:'post',
			                	dataType:'json',
			                	data:this.$content.find('form').serialize(),
			                	success:function(res){
			                		hideLoading();
			                		if(res.code == 1){
			                			$.notify('操作成功');
			                			if(item.id > 0) t.list[k] = res.item;
			                			else t.list.push(res.item);
			                			t.$forceUpdate();
			                		}else $.notify(res.msg);
			                	}
			                });
			            },
		        	},
		        	cancel:{text:'取消'}
		        }
			});
		},
		onItemBind(item){
			showLoading();
			var t = this;
            $.ajax({
            	url:"/SPAdmin/good/bind",
            	type:'post',
            	dataType:'json',
            	data: {oid:item.id,op:'cgood'},
            	success:function(res){
            		hideLoading();
            		if(res.code == 1){
                		t.cgood = item;
                		t.cgood.cid = 0;
                		t.cgood.list = res.list;
            			$('#channelItem').modal('show');
            		}else $.notify(res.msg);
            	}
            });
		},
		doItemBind(){
			if(!this.cgood.itemno || this.cgood.itemno == '') return $.notify('请填写渠道商品ID');
			showLoading();
			var t = this;
            $.ajax({
            	url:"/SPAdmin/good/bind",
            	type:'post',
            	dataType:'json',
            	data: {oid:this.cgood.id,op:'docgood',cid:this.cgood.cid,itemno:this.cgood.itemno},
            	success:function(res){
            		hideLoading();
            		if(res.code == 1){
                		t.cgood.list.push(res.item)
            			$('#channelItem').modal('show');
                		t.$forceUpdate();
            		}else $.notify(res.msg);
            	}
            });
		},
		doItemDel(k,v){
			var t = this;
			$.confirm({
				title:'温馨提示',
				content: '确认取消绑定吗？',
		        buttons:{
		        	confirm:{ 
		        		text:'确认',
		        		btnClass: "btn-primary",
		        		action: function(){
		        			showLoading();
			                $.ajax({
			                	url:"/SPAdmin/good/bind",
			                	type:'post',
			                	dataType:'json',
			                	data: {op:'unitembind',gid:v.id},
			                	success:function(res){
			                		hideLoading();
			                		if(res.code == 1){
			                			$.notify('操作成功');
			                			t.cgood.list.splice(k,1);
			                		}else $.notify(res.msg);
			                	}
			                });
			            },
		        	},
		        	cancel:{text:'取消'}
		        }
			});
		},
		onBind(k,item){
			var str = "<form>";
			str += '<div class="input-group" style="margin-bottom: 10px;">'
			str += '<div class="input-group-text">选择渠道</div>'
			str += '<select class="form-control channel" name="cid">'
			for(var i in this.channel){
				str += '<option value="'+i+'">'+this.channel[i]+'</option>'
			}
			str += '</select>'
			str += '</div>'
			if(this.type == 1){
				str += '<div class="input-group" style="margin-bottom: 10px;">'
				str += '<div class="input-group-text">电费公司</div>'
				str += '<select class="form-control extra" name="extra">'
				str += '<option value="">通用</option>'
				str += '<option value="南网">南网</option>'
				str += '<option value="国网">国网</option>'
				str += '</select>'
				str += '</div>'
			}
	    	str += '<div class="input-group">'
	    	str += '<div class="input-group-text">渠道商品ID</div>'
	    	str += '<input type="text" class="form-control itemno" name="itemno">'
	    	str += '</div></form>'
		    var t = this;
			var alert = $.confirm({
				title:'绑定渠道商品',
				content: str,
		        buttons:{
		        	confirm:{ 
		        		text:'确认',
		        		btnClass: "btn-primary",
		        		action: function(){
			        		var data = {gid:item.id,cid:this.$content.find('.channel').val(),itemno:this.$content.find('.itemno').val()};
			        		if(t.type == 1) data['extra'] = this.$content.find('.extra').val()
		        			showLoading();
			                $.ajax({
			                	url:"/SPAdmin/good/bind",
			                	type:'post',
			                	dataType:'json',
			                	data: data,
			                	success:function(res){
			                		hideLoading();
			                		if(res.code == 1){
			                			$.notify('操作成功');
			                			if(!t.list[k].items) t.$set(t.list[k],'items',[]);
		                                t.list[k].items.push(res.item);
		                                alert.close();
			                		}else $.notify(res.msg);
			                	}
			                });
			                return false;
			            },
		        	},
		        	cancel:{text:'取消'}
		        }
			});
		},
		onTip(txt){
			$.notify(txt)
		},
		onGStatus(item){
			showLoading();
            $.ajax({
            	url:"/SPAdmin/good",
            	type:'post',
            	dataType:'json',
            	data: {gid:item.id,op:'status',type:this.type},
            	success:function(res){
            		hideLoading();
            		if(res.code == 1){
            			$.notify('操作成功');
            			item.status = item.status == 0 ? 1 : 0;
            		}else $.notify(res.msg);
            	}
            });
		},
		onGDel(k){
			var t = this;
			var data = {gid:t.list[k].id};
			if(this.type < 10) data['op'] = 'item';
			$.confirm({
				title:'温馨提示',
				content: '确认删除该产品吗？',
		        buttons:{
		        	confirm:{ 
		        		text:'确认',
		        		btnClass: "btn-primary",
		        		action: function(){
		        			showLoading();
			                $.ajax({
			                	url:"/SPAdmin/good/del",
			                	type:'post',
			                	dataType:'json',
			                	data: data,
			                	success:function(res){
			                		hideLoading();
			                		if(res.code == 1){
			                			$.notify('操作成功');
			                			t.list.splice(k,1);
			                		}else $.notify(res.msg);
			                	}
			                });
			            },
		        	},
		        	cancel:{text:'取消'}
		        }
			});
		},
		onCDel(kk,dk,item){
		    var t = this;
			var alert = $.confirm({
				title:'温馨提示',
				content: '确认取消绑定渠道商品吗？',
		        buttons:{
		        	confirm:{ 
		        		text:'确认',
		        		btnClass: "btn-primary",
		        		action: function(){
		        			showLoading();
			                $.ajax({
			                	url:"/SPAdmin/good/bind",
			                	type:'post',
			                	dataType:'json',
			                	data: {op:'unbind',gid:item.id},
			                	success:function(res){
			                		hideLoading();
			                		if(res.code == 1){
			                			$.notify('操作成功');
			                			t.list[kk].items.splice(dk,1);
			                		}else $.notify(res.msg);
			                	}
			                });
			            },
		        	},
		        	cancel:{text:'取消'}
		        }
			});
		}
	}
});

function onUrl(obj,type){
	var file = $(obj)[0].files[0];
	if(!file) return;
	var reader = new FileReader();
	reader.readAsDataURL(file);
	reader.onload = function(e) {
		var img = "<div style='position: relative;'><img src='"+this.result+"' width='100px' height='100px'><input type='hidden' name='logo' value='"+this.result+"'><input type='file' accept='image/*' class='hide' onchange='onUrl(this,"+type+")' name='logo'></div>";
		if(type == 2){
			var po = $(obj).parent();
    		po.prev().after(img);
    		po.remove();
		}else{
    		$(obj).prev().after(img);
    		$(obj).remove();
		}
	}
}

function difference(obj1, obj2) {
    var result = {};
    for (var key in obj1) {
        if (obj1.hasOwnProperty(key) && !obj2.hasOwnProperty(key)) {
            result[key] = obj1[key];
        }
    }
    return result;
}
</script>
